package com.ruoyi.doc.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

public class RyFileUtil {

	 // 把File转成MultipartFile
	 	public static MultipartFile getMulFileByPath(String picPath) {
	 		FileItem fileItem = createFileItem(picPath);
	 		MultipartFile mfile = new CommonsMultipartFile(fileItem);
	 		return mfile;
	 	}

	 	public static FileItem createFileItem(String filePath) {
	 		FileItemFactory factory = new DiskFileItemFactory(16, null);
	 		String textFieldName = "textField";
	 		int num = filePath.lastIndexOf(".");
	 		String extFile = filePath.substring(num);
	 		FileItem item = factory.createItem(textFieldName, "text/plain", true, "MyFileName" + extFile);
	 		File newfile = new File(filePath);
	 		int bytesRead = 0;
	 		byte[] buffer = new byte[8192];
	 		try {
	 			FileInputStream fis = new FileInputStream(newfile);
	 			OutputStream os = item.getOutputStream();
	 			while ((bytesRead = fis.read(buffer, 0, 8192)) != -1) {
	 				os.write(buffer, 0, bytesRead);
	 			}
	 			os.close();
	 			fis.close();
	 		} catch (IOException e) {
	 			e.printStackTrace();
	 		}
	 		return item;
	 	}
}
